<%@ page language="java" import="java.util.*" import="domain.*"
         pageEncoding="UTF-8" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
    <title>购物车</title>
</head>
<body>

<table width="100%" border="0" cellspacing="0">
    <tr>
        <td>
            <table border="1" cellspacing="0" width="100%">
                <tr>
                    <td width="15">序号</td>
                    <td width="20%">商品名称</td>
                    <td width="13%">价格</td>
                    <td width="10%">库存</td>
                    <td width="28%">数量</td>
                    <td width="24">删除</td>
                </tr>
                <%
                    Double total = 0.0;
                    Set keySet = ((HashMap<String, Integer>) session.getAttribute("cart")).keySet();
                    //获取cart的keySet集合
                    Collection<Product> products = (Collection<Product>) session.getAttribute("products");
                    int i = 0;
                    for (Object key : keySet)//遍历cart的键集合
                    {
                        for (Product pro : products) { //遍历商品集合
                            if (key.equals(pro.getId())) {//如果“键”和商品的id相等
                %>
                <tr>
                    <td><%=++i%>
                    </td>
                    <td><%=pro.getName()%>
                    </td>
                    <td><%=pro.getPrice()%>
                    </td>
                    <td>$<%=pro.getPnum()%>
                    </td>
                    <td><%=((HashMap<?, ?>) session.getAttribute("cart")).get(key)%>
                        <%--获取cart中key对应的value，也即该书添加进购物车的本数--%>
                    </td>
                    <td>
                        <form
                                action="DeleteCart.jsp?pId=<%=pro.getId()%>"
                                method="post">
                            <button style='color: #FF0000; font-weight: bold'>X</button>
                        </form>
                    </td>
                </tr>
                <%
                                total = total + pro.getPrice() * (int) ((HashMap<?, ?>) session.getAttribute("cart")).get(key);
                            }
                        }
                    }
                %>

            </table>
        </td>
    </tr>

    <tr>
        <td style="text-align: right; padding-right: 40px;"><font
                style="color: #FF6600; font-weight: bold">合计：&nbsp;&nbsp;$<%=total%>元
        </font></td>
    </tr>

</table>

</body>
</html>
